Release 10.1A: OpenEdge Data Management:
SQL Development


Testing your ODBC connection on UNIX

This section contains a sample C program that can be used to test your ODBC connection. The sample program can be built at the UNIX OS prompt provided that:

To build the executable, the following command will invoke the C compiler to compile and link the executable:

Solaris and AIX:

cc -o testConnect -I$DLC/odbc/include -L$DLC/odbc/lib -lodbc test_connect.c 

HPUX:

cc -o testConnect -I$DLC/odbc/include -lCsup -lpthread –lcl -L$DLC/odbc/lib 
-lodbc test_connect.c 

Linux:

cc -o testConnect -I$DLC/odbc/include -L$DLC/odbc/lib -lodbc -lodbcinst 
test_connect.c 

Use the following sample program to test your ODBC connection, as shown:

testconnect.c
#include <stdio.h> 
#include "sql.h" /* ODBC declarations */ 
#include "sqlext.h" /* more ODBC declarations */ 
int main(int argc, char *argv[]) 
{ 
SQLRETURN sqlReturn; 
SQLHANDLE environmentHandle; 
SQLHANDLE connectionHandle; 
/* make sure we got at least 3 arguments to the exe */ 
if (argc < 4) 
{ 
printf("Insufficient parameters provided.\n"); 
printf("Usage - %s dsn userid password\n", argv[0]); 
return 1; 
} 
else 
{ 
/* got at least 3 arguments to the exe, display */ 
/* arguments with internal usage */ 
printf("DSN NAME = %s\n", argv[1]); 
printf("USER ID = %s\n", argv[2]); 
printf("PASSWORD = ****\n\n"); /* don’t show actual */ 
} 
/* allocate an ODBC environment handle */ 
sqlReturn = SQLAllocHandle(SQL_HANDLE_ENV, 
SQL_NULL_HANDLE, 
&environmentHandle); 
if (sqlReturn != SQL_SUCCESS) 
{ 
printf("Unable to allocate Environment Handle, exiting.\n"); 
return sqlReturn; 
} 
/* set the ODBC application version to 3.x */ 
sqlReturn = SQLSetEnvAttr(environmentHandle, 
SQL_ATTR_ODBC_VERSION, 
(SQLPOINTER) SQL_OV_ODBC3, 
SQL_IS_UINTEGER); 
if (sqlReturn != SQL_SUCCESS) 
{ 
printf("Unable to set ODBC Versoin to 3.x, exiting.\n"); 
SQLFreeHandle(SQL_HANDLE_ENV, environmentHandle); 
return sqlReturn; 
} 
/* allocate a database connection handle */ 
sqlReturn = SQLAllocHandle(SQL_HANDLE_DBC, 
environmentHandle, 
&connectionHandle); 
if (sqlReturn != SQL_SUCCESS 
{ 
printf("Unable to allocate Connection Handle, exiting.\n"); 
SQLFreeHandle(SQL_HANDLE_ENV, environmentHandle); 
return sqlReturn; 
} 
/* attempt to connect o the database server */ 
sqlReturn = SQLConnect(connectionHandle, 
(SQLCHAR *)argv[1], /* dsn name */ 
SQL_NTS, /* name null terminated */ 
(SQLCHAR *)argv[2], /* user id */ 
SQL_NTS, /* user id null terminated */ 
(SQLCHAR *)argv[3], /* user pwd */ 
SQL_NTS); /* pwd null terminated */ 
if (sqlReturn == SQL_SUCCESS) 
{ 
printf("Connection to %s successful!\n", argv[1]); 
/* now disconnect from server */ 
sqlReturn = SQLDisconnect(connectionHandle); 
if (sqlReturn != SQL_SUCCESS) 
{ 
printf("Unable to disconnect client from %s.\n", argv[1]); 
} 
} 
else 
{ 
printf("Unable to connect to %s.\n", argv[1]); 
} 
/* do the clean up before exiting */ 
if (connectionHandle != NULL) 
{ 
SQLFreeHandle(SQL_HANDLE_DBC, connectionHandle); 
} 
if (environmentHandle != NULL) 
{ 
SQLFreeHandle(SQL_HANDLE_ENV, environmentHandle); 
} 
return sqlReturn; 
} 

The test executable, once built, can be used to test the ability to connect to a running database server. The executable will take three parameters: DSN, user id, and user password:

testConnect sports myuser mypwd 

A successful connection, using the test executable, will result in a displayed message similar to the following:

DSN NAME = sports 
USER ID = foo 
PASSWORD = **** 
Connection to sports successful. 


Copyright © 2005 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095